﻿using System.Linq.Expressions;
using adminTemplateApi.Data;
using adminTemplateApi.Model;
using adminTemplateApi.Model.ViewModels;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace adminTemplateApi.Controllers
{
    public class ShowTypeComponenteController : BaseController<ShowTypeComponents, int>
    {
        public ShowTypeComponenteController(DBContext context) : base(context)
        {
        }

        public override async Task<ActionResult<ShowTypeComponents>> GetOne(int id)
        {
            return await db.ShowTypeComponents.Include(x => x.ShowTypeComponentPropties)
                .FirstOrDefaultAsync(x => x.Id == id);
        }

        protected override int GetIdFromObj(ShowTypeComponents obj)
        {
            return obj.Id;
        }

        protected override Expression<Func<ShowTypeComponents, bool>> getWhere(SearchVm<ShowTypeComponents> search)
        {
            if (search.Search == null)
            {
                return x => true;
            }

            return x=>
                (search.Search.Id==0 || x.Id==search.Search.Id) &&
                (string.IsNullOrEmpty(search.Search.Label) || x.Label.Contains(search.Search.Label)) &&
                (string.IsNullOrEmpty(search.Search.Component) || x.Component.Contains(search.Search.Component)) &&
                (search.Search.Enable==null || x.Enable==search.Search.Enable);
        }
    }
}
